<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Customer</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">

<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>操作面板</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane hide" lay-filter="table-search">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">邀请码</label>
                            <div class="layui-input-inline">
                                <input type="text" name="code" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
                <div class="layui-input-inline">
                    <input type="number" placeholder="请输入生成数量" name="genNum" class="layui-input" />
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">代理渠道:</label>
                    <div class="layui-input-inline">
                        <select name="gamechannel" class="layui-select">
                            <option value="">全部</option>
                            <option v-for="item in channels" :value="item.id">{{ item.name }}</option>
                        </select>
                    </div>
                </div>
                <button class="layui-btn layui-btn-sm" id="gensale" auth="sale:gen">生成邀请码</button>
                <button class="layui-btn layui-btn-sm" id="syncAdminAccount" auth="sale:gen">同步主播账号</button>

            </div>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="table-search">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">邀请码</label>
                            <div class="layui-input-inline">
                                <input type="text" name="code" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <!--<button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="import" auth="sale:import">导入</button>-->
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="export" auth="sale:export">导出</button>
    </div>
</script>
<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-normal layui-btn-xs " lay-event="edit" auth="sale:edit">编辑</a>
</script>

<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script>
    var form,$,_this;
    var channels = getEnum('channel')
    console.log('渠道列表',channels)
    new Vue({
        el: "#app",
        data: {
            channels:channels,
        },
        methods:{

        },
        created:function(){
            _this = this;
        },
        mounted:function () {
            layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
                form.render();
                table.render({
            elem: '#currentTableId',
            url: '/admin/sale/paginate',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'print' ],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'code', title: '邀请码'},
                {field: 'password', title: '密码'},
                {field: 'channel', title: '渠道ID'},
                {field: 'channelName', title: '渠道'},
                {field: 'name', title: '主播名称'},
                {field: 'createdAt', title: '创建时间'},
                {title: '操作', minWidth: 300, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var searchData = data.field;
            var channelId = $('[name=gamechannel]').val();
            if (channelId !== '') {
                searchData.gamechannel = channelId;
            } else {
                delete searchData.gamechannel;
            }
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where:searchData
            }, 'data');
            return false;
        });

        $('#gensale').click(function(){
            var num = $('[name=genNum]').val();
            var channelId = $('[name=gamechannel]').val();
            var selectedChannelName = $('[name=gamechannel]').find("option:selected").text();

            $.post('/admin/sale/gen',{num:num,channelId: channelId,name:selectedChannelName},function(res){
                if (res.code ===0 ){
                    layer.msg('成功生成了邀请码',{icon:1,time:500},function(){
                        location.reload();
                    })
                } else {
                    layer.msg(res.msg,{icon:2} )
                }
            })
        })
        $('#syncAdminAccount').click(function () {
            $.post('/admin/sale/flushSaleAdmin',{},function(res){
                if (res.code ===0 ){
                    layer.msg('同步成功',{icon:1,time:2000})
                } else {
                    layer.msg(res.msg,{icon:2} )
                }
            })
        })

        table.on('toolbar(currentTableFilter)', function (obj) {
            if(obj.event === 'export'){ //导出
                var searchData = form.val('table-search');
                searchData.page = 1;
                searchData.limit = 9999999;
                asyncDownload(
                    '/admin/sale/export',
                    searchData,
                    {token:localStorage.getItem('adminToken')}
                );
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if(obj.event==='edit'){
                layer.open({
                    title: '邀请码编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['600px', '500px'],
                    content: 'edit.html?id='+data.id,
                });
            }
        });
    });
        }

        });

</script>
</body>
</html>